package com.smfx.yxt.controller.teacher;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.RelationType;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.reqVo.ParentReqVO;
import com.smfx.yxt.service.IParentService;
import com.smfx.yxt.service.ITeacherService;
import com.smfx.yxt.utils.BeanUtils;
import com.smfx.yxt.utils.FileUtils;
import com.smfx.yxt.utils.RequestUtil;
import com.smfx.yxt.view.ParentRelStudentView;
import com.smfx.yxt.view.TeacherInfoView;
import com.smfx.yxt.wechat.model.WechatUserInfo;
import com.smfx.yxt.wechat.utils.WechatUtils;

@RequestMapping(Const.TEACHER_ROLE + MenuConst.USER_MY_URL)
@Controller
public class TeacherUserController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(TeacherUserController.class);
	@Autowired
	private ITeacherService teacherService;
	@Autowired
	private IParentService parentService;
	
	@RequestMapping(value={"","/"})
	public String my(Model model){
		if(StringUtils.isBlank(appUser().getTeacher().getHeadImgUrl())) {
			TeacherInfo teacher = new TeacherInfo();
			teacher.setId(appUser().getTeacher().getId());
			//WechatUserInfo userInfo = WechatUtils.getUserInfo(WechatUtils.getAccessToken(), appUser().getOpenid());
			//String headImgUrl = FileUtils.writeHttpFile(userInfo.getHeadimgurl(), RelationType.HEAD_IMG_URL.getCode());
			teacher.setHeadImgUrl("sys/ls.png");
			teacherService.updateById(teacher);
			appUser().getTeacher().setHeadImgUrl("sys/ls.png");
		}
		model.addAttribute("ip", RequestUtil.getIpAddr(request));
		return "teacher/my";
	}
	
	@RequestMapping("info")
	public String teacherInfo() {
		return "teacher/info";
	}
	
	@RequestMapping("resourceRefresh")
	public String resourceRefresh() {
		session.invalidate();
		return "teacher/index";
	}
	
	/**
	 * 根据教师id查询教师信息
	 * @param teacherId
	 * @return
	 */
	@RequestMapping("info/edit")
	public String infoEdit(Model model) {
		AppUser appUser = getAppUser();
		TeacherInfo teacherInfo = teacherService.selectById(appUser.getTeacher().getId());
		TeacherInfoView infoView = BeanUtils.copyAs(teacherInfo, TeacherInfoView.class);
		model.addAttribute("teacher", infoView);
		return "teacher/teacherDetail";
	}
	
	@RequestMapping("teacher/chatList")
	public String teacherChatList() {
		return "teacher/chatList";
	}
	
	@RequestMapping("student/unbind")
	public String studentUnbind(Model model) {
		AppUser appUser = getAppUser();
		List<ParentRelStudentView> list = parentService.selectRelStudentByOpenid(appUser.getOpenid());
		model.addAttribute("list", list);
		return "teacher/studentUnbind";
	}
	
	@RequestMapping("student/unbind/submit")
	@ResponseBody Object studentUnbindSubmit(ParentReqVO vo) {
		try {
			AppUser appUser = getAppUser();
			vo.setOpenid(appUser.getOpenid());
			parentService.delByRelId(vo);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--studentUnbindSubmit error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
}
